<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use DateTimeInterface,Log;

class Linkslistsminapps extends Model
{
    protected $table = 'links_lists_minapps';
    protected $primaryKey = 'id';

    public static $types = ["","通用","公众号专用","二维码专用","客服专用","点击版专用","免费用户使用"];

    protected function serializeDate(DateTimeInterface $date)
    {
        return $date->format($this->dateFormat ?: 'Y-m-d H:i:s');
    }

    public static function linkslistsminapps($users_id,$title,$type,$appid)
    {
        $query = self::select("links_lists_minapps.*","users.nike","users.avatar")->leftJoin('users', 'links_lists_minapps.users_id', '=', 'users.id')
                    ->where("links_lists_minapps.users_id",$users_id)
                    ->whereIn("links_lists_minapps.disable",[1,2,3,5]);
        if (!empty($title)) $query->where("links_lists_minapps.name","like","%".$title."%");
        if (!empty($type)) $query->where("links_lists_minapps.type",$type);
        if (!empty($appid)) $query->where("links_lists_minapps.appid",$appid);
        return $query->orderBy("links_lists_minapps.id","DESC")->paginate(25);
    }

    public static function adminLinkListMinapp($users_id,$title,$appid)
    {
        $query = self::select("links_lists_minapps.*","users.nike","users.avatar")->leftJoin('users', 'links_lists_minapps.users_id', '=', 'users.id');
        if (!empty($title)) $query->where("links_lists_minapps.name","like","%".$title."%");
        if (!empty($users_id)) $query->where("links_lists_minapps.users_id",$users_id);
        if (!empty($appid)) $query->where("links_lists_minapps.appid",$appid);
        return $query->orderBy("links_lists_minapps.id","DESC")->paginate(25);
    }

    public static function getLinksListsMinappsByAppid($appid)
    {
        return self::where("appid",$appid)->orderBy("id","DESC")->first();
    }

    public static function getLinkListMinappVersion($appid)
    {
        return self::select("version")->where("appid",$appid)->first();
    }

    public static function getLinkListMinappAll($appids)
    {
        return self::select("appid","name")->whereIn("appid",$appids)->get();
    }

    public static function getLinksListsMinappsByAppidSimple($appid)
    {
        return self::select("appid","secret","type","status","disable")->where("appid",$appid)->whereIn("disable",[1,2,3,5])->orderBy("id","DESC")->first();
    }

    public static function getLinksListsMinappsById($id)
    {
        return self::where("id",$id)->whereIn("disable",[1,2,3,5])->first();
    }

    public static function getLinksListsMinappsByOriginalId($original_id)
    {
        return self::where("original_id",$original_id)->whereIn("disable",[1,2,3,5])->first();
    }

    public static function getUseableMinapps($users_id)
    {
        return self::where("users_id",$users_id)->where("disable",3)->orderBy("id","DESC")->first();
    }

    public static function getMiniUseableMinapps($users_id,$appid)
    {
        return self::where("users_id",$users_id)->where("appid",$appid)->where("status",1)->whereIn("disable",[1,2,3,5])->first();
    }

    public static function getUsersUseableMinapps($users_id)
    {
        return self::where("users_id",$users_id)->where("status",1)->whereIn("disable",[1,3,5])->get();
    }

    public static function getUsersUseableMinappsSimple($users_id)
    {
        return self::select("appid","name","scene","miniapp_type","disable")->where("users_id",$users_id)->where("status",1)->whereIn("disable",[1,3,5])->get();
    }

    public static function getUseableDefaultMinapps()
    {
        return self::where("disable",3)->where("status",2)->orderBy("id","DESC")->first();
    }

    public static function getPlatfromUseableDefaultMinapps($type)
    {
        return self::where("disable",3)->where("status",2)->where("type",$type)->orderBy("id","DESC")->first();
    }

    public static function getUsersMinapps($users_id)
    {
        return self::where("users_id",$users_id)->whereIn("disable",[1,2,3,5])->orderBy("id","DESC")->get();
    }

    public static function getUsersAllMinapps($users_id,$name='',$limit=20)
    {
        $query = self::where("users_id",$users_id)->whereIn("disable",[1,2,3,5]);
        if (!empty($name)) $query->where("name","like","%".$name."%");
        return $query->orderBy('id', 'desc')->paginate($limit);
    }

    public static function linksListsMinappsInsert($name,$appid,$secret,$users_id,$status,$disable,$original_id,$type,$scene)
    {
        return self::insertGetId(["name"=>$name,"appid"=>$appid,"secret"=>$secret,"users_id"=>$users_id,"status"=>$status,"disable"=>$disable,"original_id"=>$original_id,"scene"=>$scene,"type"=>$type,"version"=>2,"created_at"=>time()]);
    }

    public static function linksListsMinappsThirdInsert($name,$appid,$users_id,$original_id,$version)
    {
        return self::insert(["name"=>$name,"appid"=>$appid,"users_id"=>$users_id,"original_id"=>$original_id,"scene"=>2,"miniapp_type"=>1,"version"=>$version,"visitstatus"=>1,"created_at"=>time()]);
    }

    // 自有小程序扫码授权Urlscheme功能
    public static function authLinksListsMinappsInsert($name,$appid,$users_id,$original_id)
    {
        return self::insert(["name"=>$name,"appid"=>$appid,"users_id"=>$users_id,"original_id"=>$original_id,"scene"=>1,"miniapp_type"=>4,"version"=>2,"created_at"=>time()]);
    }

    // 自有小程序扫码授权Urlscheme功能
    public static function authLinksListsMinappsUpdate($id,$name,$appid,$original_id,$users_id)
    {
        return self::where("id",$id)->update(["name"=>$name,"appid"=>$appid,"original_id"=>$original_id,"scene"=>1,"miniapp_type"=>4,"users_id"=>$users_id,"disable"=>1]);
    }


    public static function linksListsMinappsUpdate($id,$name,$appid,$secret,$users_id,$status,$disable,$original_id)
    {
        return self::where("id",$id)->update(["name"=>$name,"appid"=>$appid,"secret"=>$secret,"users_id"=>$users_id,"status"=>$status,"disable"=>$disable,"original_id"=>$original_id]);
    }

    public static function linksListsMinappsAppUpdate($id,$name,$appid,$secret,$users_id,$status,$disable,$original_id,$type,$miniapp_type,$scene)
    {
        return self::where("id",$id)->update(["name"=>$name,"appid"=>$appid,"secret"=>$secret,"status"=>$status,"disable"=>$disable,"original_id"=>$original_id,"users_id"=>$users_id,"type"=>$type,"miniapp_type"=>$miniapp_type,"scene"=>$scene]);
    }

    public static function updateLinksListsStatus($id,$disable)
    {
        return self::where("id",$id)->update(["disable"=>$disable]);
    }

    public static function updateLinksListMiniappDisable($appid,$disable,$visit_status)
    {
        return self::where("appid",$appid)->update(["disable"=>$disable,"visitstatus"=>$visit_status]);
    }

    public static function updateAllLinksListsStatus()
    {
        return self::where("disable",3)->update(["disable"=>1]);
    }

    // 获取公用未使用的小程序
    public static function getPublicUseableMinapps()
    {
        return self::where("users_id",5)->where("status",2)->where("disable",1)->orderBy("id","DESC")->first();
    }

    // 获取公用密钥未使用的小程序
    public static function getPublicUseableMinappsBySecret()
    {
        return self::where("users_id",5)->where("status",2)->where("disable",1)->where('miniapp_type',2)->orderBy("id","DESC")->first();
    }

    // 获取公用扫码授权未使用的小程序
    public static function getPublicUseableMinappsByscan()
    {
        return self::where("users_id",5)->where("status",2)->where("disable",1)->whereIn('miniapp_type',[1,4])->orderBy("id","DESC")->first();
    }

    public static function updatePublicLinksListsStatus($id,$disable,$type)
    {
        return self::where("id",$id)->update(["disable"=>$disable,"type"=>$type]);
    }

    public static function getDefaultMinapp($type)
    {
        return self::where("users_id",5)->where("status",2)->where("disable",3)->where("type",$type)->orderBy("id","DESC")->first();
    }

    public static function getDefaultPublicMinapp()
    {
        return self::where("users_id",5)->where("status",2)->where("disable",3)->whereIn('type',[3,4,5])->orderBy("id","DESC")->first();
    }

    // 获取用户的独家小程序
    public static function getUsersSoleMinapps($users_id)
    {
        return self::where("users_id",$users_id)->where("status",1)->where("disable",1)->where("scene",2)->orderBy("id","ASC")->first();
    }

    public static function updateMiniappScene($id,$scene)
    {
        return self::where("id",$id)->update(["scene"=>$scene]);
    }

    public static function deleteLinksListsMinapps($id)
    {
        return self::where("id",$id)->update(["disable"=>4,"visitstatus"=>5]);
    }

    public static function updateLinkListMiniappVisit($id,$visitstatus)
    {
        return self::where("id",$id)->update(["visitstatus"=>$visitstatus]);
    }

    public static function updateLinkListMiniappVisitAppid($appid,$visitstatus)
    {
        return self::where("appid",$appid)->update(["visitstatus"=>$visitstatus]);
    }
}